package com.lmbx.shzh.domain.common;

import com.lmbx.shzh.exception.ErrorCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Auther: wyl
 * @Date: 2021/5/11
 * @Description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "统一返回结果实体", description = "封装统一返回结果信息实体")
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 编码
     */
    @ApiModelProperty(name = "code", value = "状态码", dataType = "Integer")
    private Integer code;

    /**
     * 描述信息
     */
    @ApiModelProperty(value = "描述信息")
    private String msg;

    /**
     * 结果
     */
    @ApiModelProperty(value = "泛型结果T")
    private T data;

    /**
     * 成功
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> success() {
        return new ResponseResult(1, null, null);
    }

    /**
     * 成功
     *
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> success(T result) {
        return new ResponseResult(1, null, result);
    }

    /**
     * 成功
     *
     * @param message
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> success(String message, T result) {
        return new ResponseResult(1, message, result);
    }

    /**
     * 失败
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> error(String message) {
        return new ResponseResult(-1, message, null);
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> error(Integer code, String message) {
        return new ResponseResult(code, message, null);
    }

    /**
     * 失败
     *
     * @param codeEnum
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> error(ErrorCodeEnum codeEnum) {
        return error(codeEnum.getCode(), codeEnum.getMessage());
    }
}